package org.mydba.util;

import oracle.jdbc.pool.OracleConnectionPoolDataSource;
import oracle.jdbc.pool.OracleDataSource;
import org.h2.jdbcx.JdbcConnectionPool;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Helper class.
 *
 * @since 1.0
 */
public class DBAHelper {

    /**
     * Close a connection.
     *
     * @param c the connection
     */
    public static void close(final Connection c) {
        if (c != null) {
            try {
                c.close();
            }
            catch (SQLException ignored) {
            }
        }
    }

    /**
     * Close a statement.
     *
     * @param s the statement
     */
    public static void close(final Statement s) {
        if (s != null) {
            try {
                s.close();
            }
            catch (SQLException ignored) {
            }
        }
    }

    /**
     * Close a result set.
     *
     * @param r the result set
     */
    public static void close(final ResultSet r) {
        if (r != null) {
            try {
                r.close();
            }
            catch (SQLException ignored) {
            }
        }
    }

    /**
     * Set the transaction as readonly on the Oracle monitored connection.
     * Apply patch for bug when selecting from v$sql_plan.
     *
     * @param c the monitored Oracle connection
     */
    public static void prepareConnection(final Connection c) {
        Statement s = null;
        try {
            c.setAutoCommit(false);
            s = c.createStatement();
            s.execute("set transaction read only name 'MyDBA'");
            // to avoid ORA error with Oracle 10g when querying v$sql_plan!
            s.execute("ALTER SESSION set \"_cursor_plan_unparse_enabled\"=FALSE");
        }
        catch (SQLException ignored) {
        }
        finally {
            close(s);
        }
    }

    /**
     * Invoke a rollback of the transaction started on the given (MyDBA reporting database) connection.
     *
     * @param c the connection on which the rollback is invoked
     */
    public static void rollback(final Connection c) {
        if (c != null) {
            try {
                c.rollback();
            }
            catch (SQLException ignored) {
            }
        }
    }

    /**
     * Close a datasource.
     *
     * @param ds the datasource
     */
    public static void close(final OracleConnectionPoolDataSource ds) {
        if (ds != null) {
            try {
                ds.close();
            }
            catch (SQLException ignored) {
            }
        }
    }

    /**
     * Close a datasource.
     *
     * @param ds the datasource
     */
    public static void close(final OracleDataSource ds) {
        if (ds != null) {
            try {
                ds.close();
            }
            catch (SQLException ignored) {
            }
        }
    }

    /**
     * Close a H2 connection pool.
     *
     * @param h2ConnectionPool the H2 connection pool
     */
    public static void close(final JdbcConnectionPool h2ConnectionPool) {
        if (h2ConnectionPool != null) {
            try {
                h2ConnectionPool.dispose();
            }
            catch (SQLException ignored) {
            }
        }
    }
}
